/*
class MyArray {
    public Object[] objects = new Object[10];

    public Object getPos(int pos) {
        return objects[pos];
    }

    public void setVal(int pos, Object val) {
        objects[pos] = val;
    }
}
*/

import java.lang.reflect.Array;
import java.util.Arrays;

/*
* <T> T是一个占位符，表示当前类是一个泛型类
* 泛型存在的意义：
* 1.在编译的时候，帮我们进行类型的检查
* 2.在编译的时候，帮我们进行类型的转换
* 泛型只存在编译时期，运行时没有泛型，JVM不存在泛型
* */
class MyArray<T> {
    //public Object[] objects = new Object[10];
    //public T[] objects = new T[10];
    public T[] objects ;//这样写也不好
    public MyArray() {

    }
    public MyArray(Class<T> clazz, int capacity) {
        objects = (T[]) Array.newInstance(clazz,capacity);
    }
    public T getPos(int pos) {
        return objects[pos];
    }
    public void setVal(int pos, T val) {
        objects[pos] = val;
    }
    public T[] getArray() {
        return objects;
    }
}
public class Test2 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>(Integer.class,10);
        Integer[] integers = myArray.getArray();
        // 不允许吧object类型强制转换
        //Object[] integers = myArray.getArray();
    }
    public static void main5(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>();
        myArray.setVal(0,1);
        myArray.setVal(1,2);
        int val = myArray.getPos(1);
        System.out.println(val);
        System.out.println("========");
        MyArray<String> myArray1 = new MyArray<>();
        myArray1.setVal(0,"hello");
        myArray1.setVal(1, "world");
        String ret = myArray1.getPos(0);
        System.out.println(ret);
    }
    public static void main4(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setVal(0,"123");
        myArray.setVal(1,10);
        int val = (int)myArray.getPos(1);
        System.out.println(val);
    }
    public static void main3(String[] args) {
        Integer a = 127;
        Integer b = 127;
        Integer c = 128;
        Integer d = 128;
        System.out.println(a == b);
        System.out.println(c == d);
    }
    public static void main2(String[] args) {
        Integer i = 10;
        int a = i.intValue();//显示拆箱
        int a2 = i;//自动拆箱
    }
    public static void main1(String[] args) {
        int a = 10;
        Integer i = a;//自动装箱
        Integer i2 = Integer.valueOf(a);//显示装箱
        Integer i3 = new Integer(a);//显示装箱
    }
}
